package com.worthtech.billportal.web.cookie;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class AuthCodeCookie {
	private HttpServletRequest request;

	private HttpServletResponse response;

	public AuthCodeCookie(HttpServletRequest request,
			HttpServletResponse response) {
		this.request = request;
		this.response = response;
	}

	public String getCookies(String cookieName) {
		Cookie[] cookies = request.getCookies();
		Cookie cookie = null;
		try {
			if (cookies != null && cookies.length > 0) {
				for (int i = 0; i < cookies.length; i++) {
					cookie = cookies[i];
					if (cookie.getName().equals(cookieName)) {
						return cookie.getValue();
					}
				}

			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}

	public void addAuthCode(String authCode) {
		addCookie("AUTHCODE", authCode);
	}

	public void addCookie(String name, String value) {
		Cookie cookies = new Cookie(name, value);
		cookies.setPath("/");
		cookies.setMaxAge(-1);
		response.addCookie(cookies);
	}
	
	public String getAuthCode(){
		return getCookies("AUTHCODE");
	}

}
